/**
 * Created by zlp on 2018/01/04.
 * For 我的账户
 */
var Promise = require("bluebird");

var request = require("superagent");
var API = require("../support/const");
var setting = require("../../common/cn/member/index");
var requestUtil = require("../common/requestUtil");
let templateResult = {
    datas: {},
    setting: setting
};
//登录
exports.signup = function (_req, _res, next) {
    let {password, username} = _req.body
    let params = {
        mobile: username,
        password: password
    }

    request.put(API.sale.login.signup_email)
    .query({msg: JSON.stringify(params)})
    .end(function (req, res) {
        console.log(res);
        if (res.status == 200) {
            let datas = JSON.parse(res.text).datas
            _req.session.sale = JSON.parse(datas)
            _res.json(JSON.parse(res.text))
        } else {
            _res.json(JSON.parse(res))
        }

    });
}
//登出
exports.logout = function (req, res, next) {

    let params = {
        token: req.cookies.saletoken
    }
    request.put(API.sale.logout.signout)
        .query({msg: JSON.stringify(params)})
        .end(function (_req, _res) {
            // delete _req.session.sale
            res.json(JSON.parse(_res.text))
        });
}
//修改密码
exports.update_password = function (req, res, next) {

    let {new_password, password} = req.body
    let params = {
        password_new: new_password,
        password_old: password
    }
    let header = {
        token:req.cookies.saletoken
    };
console.log(API.sale.update_password.updte_password)
    request.put(API.sale.update_password.updte_password)
        .query({msg: JSON.stringify(params)})
        .set(header)
        .end(function (_req, _res) {
            console.log(_res.text)
            res.json(JSON.parse(_res.text))
        });
}

//忘记密码
exports.retrieve = function (req, res, next) {
    let {mobile, verify_code} = req.body
    let params = {
        mobile: mobile,
        verify_code: verify_code
    }


    request.get(API.sale.update_password.retrieve)
        .query({msg: JSON.stringify(params)})
        .end(function (_req, _res) {
            console.log(_res.text)
            res.json(JSON.parse(_res.text))
        });
}
//忘记密码--重置
exports.retrieve_reset = function (req, res, next) {
    console.log(req.body)
    let {mobile, password,reset_code} = req.body
    let params = {
        mobile: mobile,
        password: password,
        reset_code:reset_code
    }

    console.log(API.sale.update_password.retrieve_reset)
    request.put(API.sale.update_password.retrieve_reset)
        .query({msg: JSON.stringify(params)})
        .end(function (_req, _res) {
            console.log(_res.text)
            res.json(JSON.parse(_res.text))
        });
}



//客户管理
exports.saleCust = {
    //列表
    cust_list: function (req, res, next) {
        let {page,start_time, end_time,limit,mobile,customer_name,customer_no} = req.body;

        let header = {
            token:req.cookies.saletoken
        };
        let sales_id = JSON.parse(req.cookies.user).user_id;
        let params = {
            "condition": {"customer_name":customer_name,"customer_no":customer_no,"mobile":mobile,"start_time": start_time, "end_time": end_time},
            "page_size": limit,
            "page_count": 0,
            "page_no": page,
            "order_column": "",
            "order_by": ""
        };
        console.log(params);
        request.get(API.sale.sale_cust.cust)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){

                if(_res.status == 200) {
                    requestUtil.formatTableData(_res).then(function (returnData) {
                        console.log(returnData);
                        res.json(returnData)
                    })
                }
            });
    },
    cust_detail: function (req, res, next) {
        let id = req.query.cid;
        let header = {
            token:req.cookies.saletoken
        };
        let params = {
            id : id
        }
        if(id) {
            request.get(`API.sale.sale_cust.cust/${id}`)
                .query({msg:JSON.stringify(params)})
                .set(header)
                .end(function (_req, _res) {
                    if (_res.status == 200) {
                        templateResult.datas.info = JSON.parse(JSON.parse(_res.text).datas);
                    }
                });
        }

    },
}
exports.sale_deduct = {
    deduct: function (req, res, next) {
        let {page, limit, start_t, end_t} = req.query;

        let params = {
            page_no : page,
            page_size:limit,
            condition: {
                start_time: start_t,
                end_time: end_t,
            }
        };
        let header = {
            token: req.cookies.saletoken
        };
        request.post(API.sale.sale_deduct.deduct)
            .query({msg: JSON.stringify(params)})
            .set(header)
            .end(function (_req, _res) {
                requestUtil.formatTableData(_res).then(function(returnData){
                    console.log(returnData);
                    res.json(returnData);
                });
            });
    },
    deduct_detail: function (req, res, next) {

        let {page, limit,id} = req.body;
        let header = {
            token: req.cookies.saletoken
        };
        let params ={
            page_size:10,
            page_no:1,
            condition:{}
        }
        request.get(`API.sale.sale_deduct.deduct/${id}`)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function (_req, _res) {
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData);
                });
            });
    },
}